package com.pxample.pemo.config;

import com.alibaba.fastjson.JSONObject;
import io.grpc.*;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;


/**
 * 拦截器，拦截客户端带过来的请求头信息
 */
@Configuration
@GrpcGlobalServerInterceptor
public class CustomServerInterceptor implements ServerInterceptor{
    private static final Logger log = LoggerFactory.getLogger(CustomServerInterceptor.class);

    public static final Context.Key<Metadata> METADATA_KEY = Context.key("metadata");
    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata metadata, ServerCallHandler<ReqT, RespT> next) {
        Context context =Context.current().withValue(METADATA_KEY, metadata);
        log.info(JSONObject.toJSONString(context));
        // 创建一个新的ServerCallListener，将context传递给下一个handler
        return Contexts.interceptCall(context, call, metadata, next);
    }
}

